﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.ServiceModel.Activation;
using System.Threading;

namespace LSW.Wcf.App
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "StoreService" in code, svc and config file together.
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class StoreService : IStoreService
    {
        public string ExcuteStore(string NameStore, object[] Paramaters)
        {
            string result = null;
            using (MSSQLFactory conn = new MSSQLFactory())
            {
                try
                {
                    conn.open(Utility.GetConnectString());

                    SqlCommand cm = new SqlCommand(NameStore, conn.Connection);

                    cm.CommandType = CommandType.StoredProcedure;

                    SqlCommandBuilder.DeriveParameters(cm);

                    // Populate the Input Parameters With Values Provided        
                    int j = 0;
                    for (int i = 1; i < cm.Parameters.Count; i++)
                    {
                        if (Paramaters.Count() > j)
                            if (Paramaters.ElementAt(j) == null)
                                cm.Parameters[i].Value = DBNull.Value;
                            else
                                if (cm.Parameters[i].DbType == DbType.Guid)
                                {
                                    Guid guid = Guid.Parse(Paramaters.ElementAt(j).ToString());
                                    if (guid == Guid.Empty)
                                        cm.Parameters[i].Value = DBNull.Value;
                                    else
                                        cm.Parameters[i].Value = Guid.Parse(Paramaters.ElementAt(j).ToString());
                                }
                                else
                                    cm.Parameters[i].Value = Paramaters.ElementAt(j);
                        j++;
                    }

                    cm.ExecuteNonQuery();
                    foreach (SqlParameter para in cm.Parameters.Cast<SqlParameter>().Where(o => o.Direction == ParameterDirection.InputOutput))
                    {
                        if (result != null) result = result + "&";
                        result = result + (para.Value == null ? "" : para.Value.ToString());
                    }
                    
                    return result;
                }
                catch (SqlException ex)
                {
                    result = "@" + ex.Message;
                }
            }
            
            return "";
        }

        
        public string ExcuteSQL(string SQL)
        {
            string result = null;
            using (MSSQLFactory conn = new MSSQLFactory())
            {
                try
                {
                    conn.open(Utility.GetConnectString());

                    SqlCommand cm = new SqlCommand(SQL, conn.Connection);
                    cm.CommandType = CommandType.Text;
                    cm.ExecuteNonQuery();
                }
                catch (SqlException ex)
                {
                    result = "@" + ex.Message;
                }
            }

            return "";
        }
    }

}
